import sys
import os

msyn_fold=sys.argv[1]
work_fold=sys.argv[2]
cds_dict={}

for msyn in os.listdir(msyn_fold):
    if msyn.startswith('mSynF'):
        # print(msyn)
        cds_file=open(msyn_fold+'/'+msyn+'/species.gff_cds','r')
        for line in cds_file:
            if line.startswith('>'):
                gene_id=line[1:].strip()
                cds_dict[gene_id]=''
            else:
                cds_dict[gene_id]+=line
        cds_file.close()
        
        
for pseud_file in os.listdir(work_fold):
    if pseud_file.endswith('.txt.psedu'):
        out_file=open(work_fold+'/'+pseud_file+'.fa','w')
        out_dict={}
        open_file=open(work_fold+'/'+pseud_file,'r')
        for line in open_file:
            line_list=line.strip().split('\t')
            if line_list[0] not in out_dict.keys():
                out_dict[line_list[0]]=int(line_list[0])
            out_dict[line_list[0]]+=0.1
            try:
                name=sorted(line_list[4:],key=lambda x:len(cds_dict[x]),reverse=True)[0]
                fasta=cds_dict[name]
            
                out_file.write('>'+str(name)+'\n'+fasta)
            except KeyError:
                continue
        open_file.close()
    


